package com.gzist.elderlyserviceplatform.interceptors;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;


/**
 * 数据脱敏器
 */
public class MaskingSerializer extends StdSerializer<String> {

    public MaskingSerializer() {
        this(null);
    }

    public MaskingSerializer(Class<String> t) {
        super(t);
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value != null && !value.isEmpty()) {
            // 使用 StringBuilder 创建一个全为星号的字符串
            StringBuilder maskedValue = new StringBuilder();
            for (int i = 0; i < value.length(); i++) {
                maskedValue.append('*');
            }
            gen.writeString(maskedValue.toString());
        } else {
            gen.writeString(value);
        }
    }
}
